package com.woniu.market_stall_web.controller;

import com.woniu.market_stall_web.entity.User;
import com.woniu.market_stall_web.service.AuctionRoomService;
import com.woniu.market_stall_web.vo.AuctionRoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("auctionRoom")
public class RoomController {

    @Autowired
    AuctionRoomService auctionRoomService;
    @ResponseBody
    @RequestMapping("selectRoomByStallId")
    public AuctionRoomVo selectRoomByStallId(int stallId , HttpServletRequest request){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        String username = userInfo.getUsername();
        System.out.println("stallId=="+stallId);
        List<AuctionRoomVo> auctionRoomVos = auctionRoomService.queryRoomByStallId(stallId , username );
        AuctionRoomVo auctionRoomVo = auctionRoomVos.get(0);
        return auctionRoomVo;


    }


    @ResponseBody
    @RequestMapping("selectUser")
    public String selectUser(HttpServletRequest request){
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        String username = userInfo.getUsername();

        return username;
    }

    @ResponseBody
    @RequestMapping("signUp")
    public String signUp(HttpServletRequest request , @RequestParam(value = "stallId") int stallId , @RequestParam(value = "username") String username){

        auctionRoomService.signUpByUsername(stallId , username);

        return "bidding";
    }

    @ResponseBody
    @RequestMapping("bidPrice")
    public String bidPrice(HttpServletRequest request , @RequestParam(value = "stallId") int stallId , @RequestParam(value = "username") String username , @RequestParam(value = "bidPrice") int bidPrice){

        auctionRoomService.insertBidPrice(stallId , username , bidPrice);

        return "bidding";
    }

    @ResponseBody
    @RequestMapping("getMaxBidPrice")
    public int getMaxBidPrice(HttpServletRequest request , @RequestParam(value = "stallId") int stallId){

        int maxBidPrice = auctionRoomService.queryMaxBidPrice(stallId);
        System.out.println("得到的最高价格是=="+maxBidPrice);

        return maxBidPrice;
    }


    @ResponseBody
    @RequestMapping("getWinner")
    public int getWinner(HttpServletRequest request , @RequestParam(value = "stallId") int stallId , @RequestParam(value = "bidPrice") int bidPrice){

        auctionRoomService.insertAuctionOrder(bidPrice , stallId);

        return 1;
    }



    @ResponseBody
    @RequestMapping("updateAuctionStallStatus")
    public int updateAuctionStallStatus(HttpServletRequest request , @RequestParam(value = "stallId") int stallId){
        auctionRoomService.modifyAuctionStallStatus(stallId);
        return 1;
    }
}
